/*
 * © Copyright 2011 Romain Marteau, Matthieu Totet
 * 
 *    This file is part of VineaToum.
 *
 *    VineaToum is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    VineaToum is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with VineaToum.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.vineatoum.view.stateView;

import java.awt.GridBagLayout;
import javax.swing.JLabel;

import org.vineatoum.business.action.ActionDescriptor;
import org.vineatoum.business.action.abstraction.Analyse;
import org.vineatoum.business.action.analyse.Prelevement;
import org.vineatoum.business.action.operationactive.Soutirage;
import org.vineatoum.business.state.Lot;
import org.vineatoum.controller.ActionController;
import org.vineatoum.controller.IActionController;
import org.vineatoum.model.abstraction.IVineaToumModel;
import org.vineatoum.observer.IObserverAction;
import org.vineatoum.view.action.PrelevementPanel;
import org.vineatoum.view.action.SoutiragePanel;
import org.vineatoum.view.component.JAccordion;
import org.vineatoum.view.component.TrackingPanel;
import org.vineatoum.view.utils.JPanelWrapped;
import org.vineatoum.view.utils.jtablecustom.JTableCustom;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridLayout;
import java.awt.Insets;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class LotActionPanel extends JPanelWrapped implements IObserverAction {
	private static final long serialVersionUID = -5445564122111066285L;
	
	protected JLabel lblTitle;
	protected JLabel lblName;
	protected JLabel lblCuve;
	protected JLabel lblBouchons;
	protected JLabel lblQte;
	protected JLabel lblBouteilles;
	protected JLabel lblVin;
	
	protected JAccordion accordion;
	protected JPanel dataPanel;
	protected JPanel actionPanel;
	protected JTabbedPane tabPanel;
	protected TrackingPanel trackingPanel;
	protected JTableCustom<Prelevement> tablePrelevements;
	protected JTable tableRecoltes;
	protected JButton btnFaireUnPrlvement;
	protected JButton btnSoutirage;
	
	private IVineaToumModel model;
	private IActionController controller;

	/**
	 * Create the panel.
	 */
	public LotActionPanel(final IVineaToumModel model, final Lot lot) {
		DecimalFormat df = new DecimalFormat("########.##");

		this.model = model;
		this.model.addObserver(this);

		this.controller = new ActionController(model);
		
		//BoxLayout layout = new BoxLayout(this, BoxLayout.Y_AXIS);
		//SpringLayout layout = new SpringLayout();
		
		setLayout(new GridLayout(0, 1));

		dataPanel = new JPanel();
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
		gridBagLayout.rowHeights = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
		gridBagLayout.columnWeights = new double[] { 1.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 1.0,
				1.0, Double.MIN_VALUE };
		gridBagLayout.rowWeights = new double[] { 0.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0 };
		dataPanel.setLayout(gridBagLayout);
		
		{
			lblTitle = new JLabel("Vue Lot");
			GridBagConstraints gbc_lblVueMout = new GridBagConstraints();
			gbc_lblVueMout.insets = new Insets(0, 0, 5, 5);
			gbc_lblVueMout.gridx = 2;
			gbc_lblVueMout.gridy = 0;
			dataPanel.add(lblTitle, gbc_lblVueMout);
		}
		{
			lblName = new JLabel("Nom : " + lot.getName());
			GridBagConstraints gbc_lblVueMout = new GridBagConstraints();
			gbc_lblVueMout.insets = new Insets(0, 0, 5, 5);
			gbc_lblVueMout.gridx = 2;
			gbc_lblVueMout.gridy = 1;
			dataPanel.add(lblName, gbc_lblVueMout);
		}
		{
			lblCuve = new JLabel("Nb bouteilles: nb bouteilles");
			GridBagConstraints gbc_lblVueMout = new GridBagConstraints();
			gbc_lblVueMout.insets = new Insets(0, 0, 5, 5);
			gbc_lblVueMout.gridx = 2;
			gbc_lblVueMout.gridy = 2;
			dataPanel.add(lblCuve, gbc_lblVueMout);
		}
		{
			lblQte = new JLabel("Quantité embouteillé: " + lot.getQuantity());
			GridBagConstraints gbc_lblVueMout = new GridBagConstraints();
			gbc_lblVueMout.insets = new Insets(0, 0, 5, 5);
			gbc_lblVueMout.gridx = 4;
			gbc_lblVueMout.gridy = 2;
			dataPanel.add(lblQte, gbc_lblVueMout);
		}
		{
			lblBouchons = new JLabel("Bouchons -  fabricant, numéro lot, type");// + mout.getQuantity());
			GridBagConstraints gbc_lblVueMout = new GridBagConstraints();
			gbc_lblVueMout.insets = new Insets(0, 0, 5, 5);
			gbc_lblVueMout.gridx = 2;
			gbc_lblVueMout.gridy = 3;
			dataPanel.add(lblBouchons, gbc_lblVueMout);
		}
		{
			lblBouteilles = new JLabel("Bouteilles: Fabricant, type, numéro lot");// + mout.getQuantity());
			GridBagConstraints gbc_lblVueMout = new GridBagConstraints();
			gbc_lblVueMout.insets = new Insets(0, 0, 5, 5);
			gbc_lblVueMout.gridx = 2;
			gbc_lblVueMout.gridy = 4;
			dataPanel.add(lblBouteilles, gbc_lblVueMout);
		}
		{
			lblVin = new JLabel("Composition du vin : Differents vins + pourcentage");// + mout.getQuantity());
			GridBagConstraints gbc_lblVueMout = new GridBagConstraints();
			gbc_lblVueMout.insets = new Insets(0, 0, 5, 5);
			gbc_lblVueMout.gridx = 2;
			gbc_lblVueMout.gridy = 5;
			dataPanel.add(lblVin, gbc_lblVueMout);
		}

		
		add(dataPanel);

		accordion = new JAccordion();
		{
			actionPanel = new JPanel(new GridLayout(0, 1));
			
			tabPanel = new JTabbedPane();
			
			tablePrelevements = new JTableCustom<Prelevement>(new Prelevement()) {
				private static final long serialVersionUID = 1L;

				protected boolean fireEditingStarted(final JTableCustom<Prelevement> table,
						final int row, int column) {
					table.getCellEditor().stopCellEditing();
					final PrelevementPanel prelevementPanel = new PrelevementPanel(model,
							(Prelevement) table.getValueAt(row));
					Object[] options = { "OK", "Annuler" };
					int n = JOptionPane.showOptionDialog(getRootPane(), prelevementPanel,
							"Ajouter", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE,
							null, options, options[0]);
					if (n == JOptionPane.OK_OPTION) {
		
					}

					return false;
				}
			};

			// FIXME
			List<Prelevement> prelevements = new ArrayList<Prelevement>();
			for (Iterator iterator = lot.getAnalyses().iterator(); iterator.hasNext();) {
				Analyse analyse = (Analyse) iterator.next();
				prelevements.add((Prelevement) analyse);

			}
			tablePrelevements.setData(prelevements);
			
			JPanel panel = new JPanel(new BorderLayout());
			panel.add(tablePrelevements, BorderLayout.CENTER);
			panel.add(tablePrelevements.getTableHeader(), BorderLayout.NORTH);
			
			tabPanel.addTab("Prélévement", panel);
			

			tableRecoltes = new JTable();
			JPanel panelRecoltes = new JPanel(new BorderLayout());
			panelRecoltes.add(tableRecoltes, BorderLayout.CENTER);
			panelRecoltes.add(tableRecoltes.getTableHeader(), BorderLayout.NORTH);

			tabPanel.addTab("Récoltes", panelRecoltes);
			
			actionPanel.add(tabPanel);
		}
		{
			btnFaireUnPrlvement = new JButton("Faire un pr\u00E9l\u00E8vement");
			btnFaireUnPrlvement.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					final PrelevementPanel prelevementPanel = new PrelevementPanel(model,
							new Prelevement());
					Object[] options = { "OK", "Annuler" };
					int n = JOptionPane.showOptionDialog(getRootPane(), prelevementPanel,
							"Ajouter", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE,
							null, options, options[0]);
					if (n == JOptionPane.OK_OPTION) {
						Prelevement prelevement = new Prelevement();
						try {

						} catch (ClassCastException e) {
							// TODO: handle exception
							e.printStackTrace();
						}
						tablePrelevements.addRow(prelevement);
						ActionDescriptor action = new ActionDescriptor(prelevement);
						action.addVinInput(lot, 100);

						controller.addAction(action);
					}

				}
			});
//			GridBagConstraints gbc_btnFaireUnPrlvement = new GridBagConstraints();
//			gbc_btnFaireUnPrlvement.insets = new Insets(0, 0, 5, 5);
//			gbc_btnFaireUnPrlvement.gridx = 6;
//			gbc_btnFaireUnPrlvement.gridy = 14;
//			add(btnFaireUnPrlvement, gbc_btnFaireUnPrlvement);
			//actionPanel.add(btnFaireUnPrlvement);
		}
		{
			btnSoutirage = new JButton("Faire un soutirage");
			btnSoutirage.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					final SoutiragePanel soutiragePanel = new SoutiragePanel(model, new Soutirage());
					Object[] options = { "OK", "Annuler" };
					int n = JOptionPane.showOptionDialog(getRootPane(), soutiragePanel,
							"Soutirage", JOptionPane.OK_CANCEL_OPTION,
							JOptionPane.PLAIN_MESSAGE, null, options, options[0]);
					if (n == JOptionPane.OK_OPTION) {
						
					}

				}
			});
//			GridBagConstraints gbc_btnFaireUneRcolte = new GridBagConstraints();
//			gbc_btnFaireUneRcolte.insets = new Insets(0, 0, 5, 5);
//			gbc_btnFaireUneRcolte.gridx = 7;
//			gbc_btnFaireUneRcolte.gridy = 14;
//			add(btnFaireUneRcolte, gbc_btnFaireUneRcolte);
//			actionPanel.add(btnFaireUneRcolte);
			
			JPanel btnPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
			btnPanel.add(btnFaireUnPrlvement);
			btnPanel.add(btnSoutirage);
			
			actionPanel.add(btnPanel);
		}
		{
			trackingPanel = new TrackingPanel(lot);

			accordion.addBar("Action", actionPanel);
			accordion.addBar("Historique", trackingPanel);
//			GridBagConstraints gbc_panel_1 = new GridBagConstraints();
//			gbc_panel_1.gridheight = 5;
//			gbc_panel_1.insets = new Insets(0, 0, 5, 5);
//			gbc_panel_1.fill = GridBagConstraints.BOTH;
//			gbc_panel_1.gridx = 2;
//			gbc_panel_1.gridy = 16;
//			add(panel_1, gbc_panel_1);
			
			add(accordion, BorderLayout.CENTER);
		}
	}

	@Override
	public void doWhenWrapperCancelClick() {
		// TODO Auto-generated method stub

	}

	@Override
	public void doWhenWrapperOkClick() {
		// TODO Auto-generated method stub

	}
	
	

}
